using System.IO;
using UnityEngine;

namespace WolfCarol.AvgToolkit.Save
{
    public interface ISavable<T>
    {
        public void Clear();

        public T Save();

        public void Load(T obj);
    }

    public abstract class SaveManager<T, U> : MonoBehaviour where T : ISavable<U> where U : new()
    {
        public T savable;

        public string savePath;

        public void Save()
        {
            if (savable != null && savePath != string.Empty)
            {
                File.WriteAllText(savePath, JsonUtility.ToJson(savable.Save(), true));
            }
        }

        public void Load()
        {
            if (savable != null && savePath != string.Empty)
            {
                savable.Load(JsonUtility.FromJson<U>(File.ReadAllText(savePath)));
            }
        }
    }
}